package com.studyv3.test.controller.stock;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.studyv3.common.core.domain.PageBean;
import com.studyv3.common.core.domain.R;
import com.studyv3.test.domain.Products;
import com.studyv3.test.mapper.ProductsMapper;
import com.studyv3.test.service.ProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductsService productsService;

    @Autowired
    private ProductsMapper productsMapper;

    //增
    @PostMapping
    public R add(@RequestBody Products add){
        return productsService.add(add);
    }

    //删
    @DeleteMapping("/{ids}")
    public R deleteAll(@PathVariable Integer[] ids){
        productsService.deleteUserByIds(ids);
        return R.ok("删除成功");
    }

    //查所有
    @GetMapping("all")
    public R all(){
        return productsService.queryAll();
    }

    //分页查
    @GetMapping
    public R<PageBean<Products>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) String a,
            @RequestParam(required = false) String b,
            @RequestParam(required = false) String c
    ){
        System.out.println(a+b+c);
        PageBean<Products> pb = productsService.list(pageNum,pageSize, a, b, c);
        return R.ok(pb);
    }

    //改
    @PutMapping
    public R update(@RequestBody Products buyProducts) {
        return productsService.update(buyProducts);
    }



    //导出
    @PostMapping("export")
    public void exportExcel(HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("商品列表", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-Disposition","attachment;filename="+System.currentTimeMillis()+fileName+".xlsx");
        List<Products> buyProducts = productsMapper.selectList(null);
        EasyExcel.write(response.getOutputStream(),Products.class).sheet().doWrite(buyProducts);
    }

    //导入
    @PostMapping("/import")
    public R upload(@RequestPart MultipartFile file) throws IOException {
        if (!file.isEmpty()){
            EasyExcel.read(file.getInputStream(), Products.class, new ReadListener<Products>() {
                List<Products> cachedDataList =new ArrayList<>(1000);

                @Override
                public void invoke(Products add, AnalysisContext analysisContext) {
                    cachedDataList.add(add);
                    if (cachedDataList.size() >= 1000){
                        //向数据库中存一次数据
                        productsMapper.batchInsert(cachedDataList);
                        //存储完成清理list
                        cachedDataList =new ArrayList<>(1000);
                    }
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                    if (cachedDataList.size()>0){
                        productsMapper.batchInsert(cachedDataList);
                    }
                }
            }).sheet().doReadSync();
            return R.ok();
        }
        return R.fail("异常");
    }

    @GetMapping("/yj")
    public List<Integer> yj(){
        return productsMapper.yj();
    }

}
